//
//  LoginVC.swift
//  NewDelivery
//
//  Created by 天下宅 on 2016/11/4.
//  Copyright © 2016年 天下宅. All rights reserved.
//

import UIKit
import SVProgressHUD
class LoginVC: BaseVC {

    let accountTF = UITextField()
    let passwdTF = UITextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let nav = WKNavView.creatNavView(WithTitle: "登录", vProtocol: self)
        nav.leftBtnEnable = false
        self.view.addSubview(nav)
        
        self.view.backgroundColor = UIColor.groupTableViewBackground
        
        let v = UIView.init(frame: CGRect.init(x: 0, y: 84, width: screenWidth, height: 100));
        v.addLine(WithColor: UIColor.lightGray, model: [.top,.mid,.bottom])
        self.view.addSubview(v)
        
        let hintLabel = UILabel()
        hintLabel.text = "账号"
        hintLabel.font = UIFont.systemFont(ofSize: 18)
        v.addSubview(hintLabel)
        hintLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.height.equalTo(50)
            make.width.equalTo(50)
            make.left.equalToSuperview().offset(10)
        }
        
        v.addSubview(accountTF)
        accountTF.snp.makeConstraints { (make) in
            make.left.equalTo(hintLabel.snp.right)
            make.top.bottom.equalTo(hintLabel)
            make.right.equalToSuperview()
        }
        
        accountTF.placeholder = "请输入账号"
//        accountTF.wkMaxCount = 11
//        accountTF.enableMaxCount()
        
//        accountTF.text = "18381084896"
//        passwdTF.text = "123456"
        passwdTF.placeholder = "请输入密码"
        passwdTF.isSecureTextEntry = true
        
        
        
        let hint1Label = UILabel()
        hint1Label.text = "密码"
        hint1Label.font = UIFont.systemFont(ofSize: 18)
        v.addSubview(hint1Label)
        hint1Label.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.height.equalTo(50)
            make.width.equalTo(50)
            make.left.equalToSuperview().offset(10)
        }
        
        v.addSubview(passwdTF)
        passwdTF.snp.makeConstraints { (make) in
            make.left.equalTo(hint1Label.snp.right)
            make.top.bottom.equalTo(hint1Label)
            make.right.equalToSuperview()
        }
        
        
        
        let btn = UIButton.button(withTitle: "登录", bgColor: MainColor)
        btn.layer.cornerRadius = 5
        self.view.addSubview(btn)
        btn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.top.equalTo(v.snp.bottom).offset(20)
            make.height.equalTo(50)
        }
        btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        
//        let findpasswdBtn = UIButton.button(withTitle: "忘记密码？", bgColor: UIColor.clear)
//        findpasswdBtn.setTitleColor(MainColor, for: .normal)
//        self.view.addSubview(findpasswdBtn)
//        findpasswdBtn.snp.makeConstraints { (make) in
//            make.right.equalTo(btn)
//            make.top.equalTo(btn.snp.bottom).offset(10)
//            make.height.equalTo(50)
//            make.width.equalTo(100)
//        }
//        findpasswdBtn.addTarget(self, action: #selector(findpasswd), for: .touchUpInside)
        
        if let str = WKLocalDataManager.wkObjectForKey(key: ACCOUNT) as? String
        {
            if str.characters.count > 0
            {
                accountTF.text = str
            }
        }
        
        if let str = WKLocalDataManager.wkObjectForKey(key: PASSWD) as? String
        {
            if str.characters.count > 0
            {
                passwdTF.text = str
            }
        }
        NotificationCenter.default.addObserver(self, selector: #selector(login), name: NSNotification.Name(rawValue: URLUpdate), object: nil)
        NetWorkingManager.getUrl()
    }
    
    func login() {
        if accountTF.text!.characters.count > 0 && passwdTF.text!.characters.count > 0
        {
            self.btnClick()
        }
        else
        {
            SVProgressHUD.dismiss()
        }
    }
    
    

    override func viewWillAppear(_ animated: Bool) {
        if let str = WKLocalDataManager.wkObjectForKey(key: ACCOUNT) as? String
        {
            if str.characters.count > 0
            {
                accountTF.text = str
            }
        }
        
        if let str = WKLocalDataManager.wkObjectForKey(key: PASSWD) as? String
        {
            if str.characters.count > 0
            {
                passwdTF.text = str
            }
        }
        


    }
    
    func findpasswd()
    {
        let vc = FindPasswdVC()
        self.present(vc, animated: true, completion: nil)
    }
    
    func btnClick()
    {
        if accountTF.text?.characters.count ?? 0 < 11
        {
            self.view.wkMakeToastToCenter(message: "手机号有误")
            return
        }
        
        if passwdTF.text?.characters.count ?? 0 < 6
        {
            self.view.wkMakeToastToCenter(message: "密码位数应大于等于6位")
            return
        }
        
        
        NetWorkingManager.login(WithUName: accountTF.text!, pwd: passwdTF.text!, hintStr: "登录中", block: {(state, data) in
            switch state
            {
            case .Faild(let msg):
                self.view.wkMakeToastToCenter(message: msg)
            case .Success:
                let _ = WKUserInfo.sharedInstance(WithDict: data!.dictionaryValue)
                isLogin = true
                WKMapManager.shard.startLoction()
                WKLocalDataManager.wksetObject(value: self.accountTF.text!, forKey: ACCOUNT)
                WKLocalDataManager.wksetObject(value: self.passwdTF.text!, forKey: PASSWD)
                self.passwdTF.text = ""
                self.accountTF.text = ""
                let vc = MainVC()
                self.present(vc, animated: true, completion: nil)
                
                
            }
        })
        

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}

